﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UdpAsyncEcho
{
    internal interface IContentProvider
    {
        string NextMessage { get; }
        bool Receive(string msg);
    }

    sealed class BatchContentProvider : IContentProvider
    {
        private readonly string m_prefix;
        private readonly int m_batchSize;
        private volatile int m_counter;
        private volatile int m_batchCounter; 

        public BatchContentProvider(string prefix, int batchSize)
        {
            m_prefix = prefix;
            m_batchSize = batchSize;
            m_counter = 0;
            m_batchCounter = 0;
        }

        public string NextMessage
        {
            get
            {
                if (m_counter >= m_batchSize)
                {
                    m_counter = 0;
                    return null;
                }
                else
                {
                    ++m_counter;
                    return string.Format("{0}-{1}", m_prefix,m_batchCounter * 100 +  m_counter);
                }
            }
        }

        public bool Receive(string msg)
        {
            ++m_counter;
            Console.WriteLine("{0}-th: {1}",m_counter,msg);

            if (m_counter >= m_batchSize)
            {
                m_counter = 0;
                ++m_batchCounter;

                Console.WriteLine();
                return false;
            }
            else
                return true;
        }
    }// BatchContentProvider
}
